package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

type QueryJob struct {
	Id      int64
	QueryId string `orm:"size(32)"`
	//Username    string    `orm:"size(32);index"`
	SubmitTime time.Time `orm:"auto_now_add;type(datetime)"`
	FinishTime time.Time `orm:"auto_now_add;type(datetime)"`
	Type       string    `orm:"size(10);index"`  // exact, sub, sim, text
	Parameters string    `orm:"type(text);null"` // query string and other parameters
	Status     string    `orm:"size(10)"`        // submit, run, finish
	NumHits    int64
	Result     string `orm:"type(text);null"` // all unique IDs of hits in one string separated by ";"
}

func (m *QueryJob) TableName() string {
	return "queryjob"
}

func (m *QueryJob) Insert() error {
	if _, err := orm.NewOrm().Insert(m); err != nil {
		return err
	}
	return nil
}

func (m *QueryJob) Read(fields ...string) error {
	if err := orm.NewOrm().Read(m, fields...); err != nil {
		return err
	}
	return nil
}

func (m *QueryJob) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(m, fields...); err != nil {
		return err
	}
	return nil
}

func (m *QueryJob) Delete() error {
	if _, err := orm.NewOrm().Delete(m); err != nil {
		return err
	}
	return nil
}

func (m *QueryJob) Query() orm.QuerySeter {
	return orm.NewOrm().QueryTable(m)
}
